<?php


namespace app\blog\model;


use think\Model;

/**
 * 分类model
 * Class LabelModel
 * @package app\blog\model
 */
class LabelModel extends Model
{
    protected $name = "label";

    /**
     * 前台展示的单个分类下文章
     * @param $label
     * @return array|\PDOStatement|string|\think\Collection|\think\model\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function lst($label)
    {
        $label = self::where('label', $label)->find();
        $id = $label['id'];
        $lst = ArticleModel::where('find_in_set('.$id.',label)')
            ->select();
        return $lst;
    }

    /**
     * 前台展示的所有标签分类
     * @return array
     */
    public static function lst_labels()
    {
        $lst = self::where('article_num','>',0)->select();
        $labels = array();
        foreach ($lst as $vo) {
            $id = $vo['id'];
            $number = ArticleModel::where('find_in_set('.$id.',label)')->count();
            $labels[] = ['label' => $vo['label'], 'number' => $number];
        }
        return $labels;
    }
}